

//
//  StatusCell.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/7.
//  Copyright © 2015年 002. All rights reserved.
//

#import "StatusCell.h"
#import "masonry.h"
#import "UIView+Extention.h"
#import "UILabel+Extention.h"

@implementation StatusCell
static NSString * const reuseIdentifier = @"StatusCellNormalId";

/**
 *  根据视图模型设置顶部视图
 *
 *  @param viewModel 微博视图模型
 */
- (void)setViewModel:(StatusViewModel *)viewModel {
    if (viewModel) {
        self.topView.viewModel = viewModel;
        self.contentLabel.text = viewModel.status.text;
        
        // 配图视图 － 设置视图模型之后，配图视图有能力计算大小
        self.pictureView.viewModle = viewModel;
        
        [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo([NSNumber numberWithFloat:self.pictureView.width]);
            make.height.equalTo([NSNumber numberWithFloat:self.pictureView.height]);
            
        }];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

// 设置界面
- (void)setupUI {
    
//    self.backgroundColor = [UIColor orangeColor];
    
    // 1. 添加控件
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.pictureView];
    [self.contentView addSubview:self.bottomView];
    
    // 设置换行宽度
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 2 * StatusCellMagrin;
    
    // 2. 布局
    // 顶部视图
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.equalTo([NSNumber numberWithInt:(2 * StatusCellMagrin + StatusIconWidth.intValue)]);
    }];
    
    // 正文文字
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(StatusCellMagrin);
        make.left.equalTo(self.contentView.mas_left);
    }];

    // 底部视图
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureView.mas_bottom).offset(StatusCellMagrin);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.equalTo(@44);
    }];
}

/**
 *  根据视图模型计算行高
 *
 *  @param vm 微博视图模型
 *
 *  @return 行高
 */
- (CGFloat)rowHeight:(StatusViewModel *)vm {
    
    // 1. 设置视图模型 （会调用 setViewModel） 设置内容以及更新约束
    self.viewModel = vm;
    
    // 2. 更新约束
    [self layoutIfNeeded];
    
    // 3. 返回底部视图最大高度
    return CGRectGetMaxY(self.bottomView.frame);
}

#pragma mark - 懒加载控件
- (StatusCellTopView *)topView {
    if (!_topView) {
        _topView = [[StatusCellTopView alloc] init];
    }
    return _topView;
}

- (StatusCellBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[StatusCellBottomView alloc] init];
    }
    return _bottomView;
}

// 微博文字
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel initWithTitle:@"微博文字" fontSize:14 color:[UIColor darkGrayColor] screenInset:0];
    }
    return _contentLabel;
}

// 配图视图
- (StatusCellPictureView *)pictureView {
    if (!_pictureView) {
        _pictureView = [[StatusCellPictureView alloc] init];
    }
    return _pictureView;
}

@end
